<!--
 * @Descripttion:  
 * @version:  
 * @Author: chenxf
 * @Date: 2022-11-25 17:21:10
-->
<template>
  <div>
    <hc-upload
      :uploadType="2"
      :partSize="1024 * 1024 * 10"
      :token="token"
      :multiple="true"
      :uploadUrl="url"
      :fileList.sync="fileList"
      :beforeUpload="beforeUpload"
      :uploadApiPath="uploadApiPath"
      @delete="deleteEvent"
      @uploadError="uploadErrorEvent"
    ></hc-upload>
  </div>
</template>

<script>
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { objectPath, deleteObject, objectPrefix } from './JossService.js'
export default {
  components: {},
  data() {
    return {
      //实例原始数据只有url的时候
      rawFileData: [
        'http://120.35.11.49:6111/joss/hcc-dev-test/property/upload-path/2022-12/015a78d3-f807-4f8e-b027-28c8a30edb61.png'
      ],
      //fileList
      //格式
      //{
      // url: xx,
      // jossToken: xx 可填 在joss必填
      // }
      fileList: [],
      url: [], //自己使用before添加地址的时候需要
      uploadApiPath: 'property/upload-path'
    }
  },
  computed: {
    token() {
      return Vue.ls.get(ACCESS_TOKEN)
    }
  },
  created() {
    this.fileList = this.rawFileData.map(i => {
      return { url: i }
    })
    // this.getJOSSToken()
  },
  methods: {
    deleteEvent(row, calback) {
      deleteObject('property/upload-path', {
        url: row.url.split('?')[0] //防止url带参数了
      })
        .then(res => {
          calback({ success: true })
        })
        .catch(() => {
          this.$message.error('删除失败')
          calback({ success: false })
        })
    },
    uploadErrorEvent() {
      this.$message.error('上传失败')
    },
    //业务系统
    beforeUpload(e) {
      // return Promise
      return new Promise(resolve => {
        let list = Array.prototype.slice.call(e)
        let infos = []
        list.forEach(i => {
          infos.push({
            objectSize: i.size,
            contextType: i.type,
            fileName: i.name,
          })
        })
        const params = {
          preSign: '',
          infos
        }
        objectPath(this.uploadApiPath, params).then(res => {
          if (res.infos) {
            this.url = res.infos
            resolve()
          }
        })
      })
    },
    getJOSSToken() {
      const params = {
        modulePath: '',
        preSign: ''
      }
      objectPrefix(this.uploadApiPath, params).then(res => {
        this.fileList = this.rawFileData.map(i => {
          return { url: i, jossToken: res.jossToken }
        })
      })
    }
  }
}
</script>
<style lang="less" scoped></style>
